﻿using AutoUpdaterDotNET;
using IWshRuntimeLibrary;
using PrinterMonitorSvc.Core;
using PrinterMonitorSvc.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrinterMonitorSvc.Common
{
    /// <summary>
    /// 系统帮助类
    /// </summary>
    public static class SystemHelper
    {
        /// <summary>
        /// 自动更新
        /// </summary>
        public static void AutoUpdate()
        {
            AutoUpdater.InstallationPath = SystemConstant.APP_DIRECTORY;
            AutoUpdater.UpdateFormSize = new System.Drawing.Size(1100, 1000);
            AutoUpdater.Synchronous = true;
            AutoUpdater.ShowSkipButton = false;

            //自动更新
            AutoUpdater.Start("http://download.banggood.cn/PrinterMonitorSvc/PrinterMonitorSvc.xml");
        }

        /// <summary>
        /// 创建桌面快捷方式
        /// </summary>
        /// <returns>成功或失败</returns>
        public static bool CreateDesktopShortcut()
        {
            try
            {
                string fileName = "PrinterMonitorSvc";
                string exePath = Process.GetCurrentProcess().MainModule.FileName;
                string deskTop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\";
                if (System.IO.File.Exists(deskTop + fileName + ".lnk"))  //
                {
                    System.IO.File.Delete(deskTop + fileName + ".lnk");//删除原来的桌面快捷键方式
                }
                WshShell shell = new WshShell();

                //快捷键方式创建的位置、名称
                IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(deskTop + fileName + ".lnk");
                //目标文件
                shortcut.TargetPath = exePath;
                //该属性指定应用程序的工作目录，当用户没有指定一个具体的目录时，快捷方式的目标应用程序将使用该属性所指定的目录来装载或保存文件。
                shortcut.WorkingDirectory = Environment.CurrentDirectory;
                //目标应用程序的窗口状态分为普通、最大化、最小化【1,3,7】
                shortcut.WindowStyle = 1;
                //描述
                shortcut.Description = fileName;
                //快捷方式图标
                shortcut.IconLocation = Environment.CurrentDirectory + "\\print.ico";
                shortcut.Arguments = "";
                // 快捷键
                //shortcut.Hotkey = "CTRL+ALT+F11"; 
                //必须调用保存快捷才成创建成功
                shortcut.Save();
                return true;
            }
            catch (Exception ex)
            {
                MonitorContext.LogError($"创建桌面快捷方式失败：{ex}");
                return false;
            }
        }

        /// <summary>
        /// 清理过期日志
        /// </summary>
        public static void ClearOverdueLog()
        {
            Task.Run(()=> {
                try
                {
                    var logDirectory = Environment.CurrentDirectory + "\\log";
                    var fileList = FileHelper.GetAllFiles(logDirectory, false);
                    foreach (var file in fileList)
                    {
                        if (file.CreationTime.Date < DateTime.Now.AddDays(-3).Date)
                        {
                            file.Delete();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MonitorContext.LogError($"清理过期日志失败：{ex}");
                }
            });

        }

        /// <summary>
        /// 清理过期PDF
        /// </summary>
        public static void ClearOverduePdf()
        {
            Task.Run(()=> {
                try
                {
                    var pdfDirectory = Path.GetTempPath();
                    var fileList = FileHelper.GetAllFiles(pdfDirectory, false);
                    foreach (var file in fileList)
                    {
                        if (file.CreationTime.Date < DateTime.Now.AddDays(-2).Date && file.Extension.ToLower() == ".pdf")
                        {
                            file.Delete();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MonitorContext.LogError($"清理过期PDF失败：{ex}");
                }
            });

        }
    }
}
